from wtforms import Form, StringField, TextAreaField, SubmitField
import wtforms
from wtforms.validators import Email, Length, EqualTo, DataRequired

from model import Usermodel


class RegisterForm(wtforms.Form):
    email = wtforms.StringField(validators=[Email(message="邮箱格式错误"), DataRequired()])
    username = wtforms.StringField(validators=[Length(min=3, max=10, message="用户名长度不符合要求"), DataRequired()])
    password = wtforms.StringField(validators=[Length(min=8, max=20, message="密码过长不符合要求"), DataRequired()])
    password_confirm = wtforms.StringField(validators=[EqualTo("password"), DataRequired()])

    # 自定义验证：
    # 验证邮箱是否被注册
    def validate_email(self, field):
        email = field.data
        user = Usermodel.query.filter_by(email=email).first()
        if user:
            # 手动设置异常
            raise wtforms.ValidationError(message='该邮箱已经被注册！')

class LoginForm(wtforms.Form):
    email = wtforms.StringField(validators=[Email(message="请输入正确的邮箱！"), DataRequired()])
    password = wtforms.StringField(validators=[DataRequired(message="请输入密码！"), DataRequired()])

class ProfileForm(wtforms.Form):
    bio = wtforms.TextAreaField('简介:', validators=[Length(max=500)])